
/*****************************************************************/
/*****************************************************************/
///***       This creates estimation_file          ***///
/*****************************************************************/
/*****************************************************************/


/* merge allspeakers (leaders in UNGA) with country variables */
clear
use vars/variables
replace iso3n=200 if country=="Czech Republic" & year<1993
drop country_text_id country_id country ccode stateabb
/* drop Somaliland and other country duplicates */
drop if country_name==162
replace iso3n=886 if NAMES_STD =="Yemen" & year<1970
sort iso3n year
save vars/vars, replace

use vars/allspeakers
sort iso3n year
drop if country=="European Union" | country=="Palestine" | country=="Vatican"

merge iso3n year using vars/vars, nokeep
drop _merge
sort ccode year


/* communist regimes dummy */
drop communist
gen communist=0
replace communist=1 if country=="Cuba" & year>1961
replace communist=1 if country=="China" & year>1949
replace communist=1 if country=="Grenada" & year>1978 & year<1984
replace communist=1 if country=="Republic of the Congo" & year>1969 & year<1991
replace communist=1 if country=="Congo" & year>1969 & year<1991
replace communist=1 if country=="Angola" & year>1974 & year<1991
replace communist=1 if country=="Mozambique" & year>1974 & year<1990
replace communist=1 if country=="Ethiopia" & year>1974 
replace communist=1 if country=="Somalia" & year>1968 & year<1978
replace communist=1 if country=="South Yemen" & year>1967  
replace communist=1 if country=="Afghanistan" & year>1978 & year<1990
replace communist=1 if country=="Mongolia" & year>1924 & year<1991
replace communist=1 if country=="North Korea" & year>1948
replace communist=1 if country=="Vietnam" & year>1975
replace communist=1 if country=="Laos" & year>1975
replace communist=1 if country=="Cambodia" & year>1979 & year<1988
replace communist=1 if country=="Benin" & year>1975 & year<1990
replace communist=1 if country=="Nicaragua" & year>1979 & year<1989
replace communist=1 if country=="Russia" & year>1917 & year<1991
replace communist=1 if country=="Belarus" & year>1917 & year<1991
replace communist=1 if country=="Ukraine" & year>1917 & year<1991
replace communist=1 if country=="Bulgaria" & year>1948 & year<1991
replace communist=1 if country=="Czechoslovakia" & year>1948 & year<1991
replace communist=1 if country=="Czech Republic" & year>1948 & year<1991
replace communist=1 if country=="German Democratic Republic" & year>1948 & year<1991
replace communist=1 if country=="East Germany" & year>1948 & year<1991
replace communist=1 if country=="Hungary" & year>1948 & year<1991
replace communist=1 if country=="Poland" & year>1948 & year<1991
replace communist=1 if country=="Romania" & year>1948 & year<1991
replace communist=1 if country=="Yugoslavia" & year>1948 & year<1991
replace communist=1 if country=="Serbia" & year>1948 & year<1991
replace communist=1 if country=="Albania" & year>1948 & year<1991
label var communist "Communist regime"

save estimation_file, replace

/* distance to NYC */
use vars/distance
ren iso_d isocode
drop iso_o contig comlang_off comlang_ethno colony comcol curcol col45 smctry
replace isocode="COD" if isocode=="ZAR"
replace isocode="ROU" if isocode=="ROM"
sort isocode
save vars/dist, replace

use estimation_file
ren NAMES_STD countryname
kountry ccode, from(cown) to(iso3c)
ren _ISO3C_ isocode
replace isocode="CZE" if country=="Czechoslovakia"
replace isocode="DDR" if country=="East Germany"
replace isocode="DEU" if country=="Germany"
replace isocode="ETH" if country=="Ethiopia"
replace isocode="MNE" if country=="Montenegro"
replace isocode="YEM" if country=="Yemen"
replace isocode="YEM" if country=="Yemen Arab Republic"
replace isocode="YMD" if country=="South Yemen"
replace isocode="SSD" if country=="South Sudan"

sort isocode
merge isocode using vars/dist, nokeep
sort ccode year
drop _merge

/* correct error for the host country */
replace distcap=328 if country=="United States"
gen logdistcap=log10(distcap)

save estimation_file, replace


/* add population and country area */
sort iso3n year
merge iso3n year using vars/areapop, nokeep
drop _merge

gen logpop=log10(PopulationtotalSPPOPTOTL)
gen logarea=log10(area1)
gen log10income=log10(GDPpercapitaconstant2010US)
ren  GDPgrowthannualNYGDPMKT wdigrowth 
gen small=0
replace small=1 if PopulationtotalSPPOPTOTL<500000 & PopulationtotalSPPOPTOTL!=.
gen vsmall=0
replace vsmall=1 if PopulationtotalSPPOPTOTL<100000 & PopulationtotalSPPOPTOTL!=.
gen large=0
replace large=1 if PopulationtotalSPPOPTOTL>100000000 & PopulationtotalSPPOPTOTL!=.


/* regional categories */
gen easteuro=1 if postsoviet==1
replace easteuro=1 if cee==1
gen oceania=0
replace west=1 if country=="Andorra"
replace latam=1 if country=="Antigua and Barbuda"
replace latam=1 if country=="Bahamas"
replace latam=1 if country=="Belize"
replace asia=1 if country=="Brunei"
replace latam=1 if country=="Dominica"
replace latam=1 if country=="Grenada"
replace oceania=1 if country=="Kiribati"
replace west=1 if country=="Liechtenstein"
replace oceania=1 if country=="Micronesia"
replace oceania=1 if country=="Marshall Islands"
replace west=1 if country=="Monaco"
replace oceania=1 if country=="Nauru"
replace oceania=1 if country=="Palau"
replace mena=1 if country=="Palestine"
replace oceania=1 if country=="Saint Lucia"
replace oceania=1 if country=="Saint Kitts and Nevis"
replace oceania=1 if country=="Saint Vincent and the Grenadines"
replace oceania=1 if country=="Samoa"
replace west=1 if country=="San Marino"
replace oceania=1 if country=="Tuvalu"
replace oceania=1 if country=="Tonga"
replace west=1 if country=="Vatican"
replace west=0 if west==.
replace africa=0 if africa==.
replace asia=0 if asia==.
replace cee=0 if cee==.
replace latam=0 if latam==.
replace mena=0 if mena==.
replace postsoviet=0 if postsoviet==.
replace oceania=1 if country=="Vanuatu"
replace asia=0 if country=="Vanuatu"
replace oceania=1 if country=="Solomon Islands"
replace asia=0 if country=="Solomon Islands"
replace oceania=1 if country=="Papua New Guinea"
replace asia=0 if country=="Papua New Guinea"
replace oceania=1 if country=="Fiji"
replace asia=0 if country=="Fiji"
replace cee=1 if country=="Czechoslovakia"
replace cee=1 if country=="East Germany"
replace cee=1 if country=="Montenegro"
replace west=1 if country=="European Union"
replace africa=1 if country=="South Sudan"
replace mena=1 if country=="South Yemen"
replace mena=1 if country=="Yemen Arab Republic"
replace asia=0 if oceania==1
replace west=1 if country=="Malta"
replace west=1 if country=="Andorra"
replace easteuro=0 if west==1
gen reg="Africa" if africa==1
replace reg="Asia" if asia==1
replace reg="East Europe" if easteuro==1
replace reg="Latin America" if latam==1
replace reg="Middle East" if mena==1
replace reg="Oceania" if oceania==1
replace reg="West" if west==1
replace reg="East Europe" if country=="East Germany"
label var africa "Africa"
label var asia "Asia"
label var latam "Latin America"
label var mena "MENA"
label var easteuro "East Europe"
label var oceania "Oceania"
label var polity2 "Polity2"
label var distcap "Geographic distance"
label var logdistcap "Geographic distance"
label var logpop "Population size (logged)"
label var log10income "GDP per capita, logged"
label var wdigrowth "GDP growth"
label var  v2xnp_pres "Presidentialism"

save estimation_file, replace

/* add democracy and check missing values */
use vars/polity
drop iso3n scode country durable
sort ccode year
save vars/proba, replace

use estimation_file
ren polity2 polity2v1
sort ccode year
merge ccode year using vars/proba, nokeep
replace polity2v1=polity2 if polity2v1==. & polity2!=.
drop _merge polity2
save estimation_file, replace

use vars/polity
replace iso3n=810 if country=="USSR"
replace iso3n=231 if country=="Ethiopia"
replace iso3n=704 if country=="Vietnam"
replace iso3n=890 if country=="Serbia"
replace iso3n=890 if ccode==347
replace iso3n=499 if country=="Montenegro"
drop ccode scode country durable
sort iso3n year
save vars/proba, replace

use estimation_file
sort iso3n year
merge iso3n year using vars/proba, nokeep
/* replace missing democracy values for USSR and BSSR with USSR values */
replace v2x_regime=0 if country=="Belarus" & year<1990
replace v2x_regime=0 if country=="Ukraine" & year<1990
replace polity2=-9 if country=="Ukraine" & year<1953
replace polity2=-9 if country=="Belarus" & year<1953
replace polity2=-7 if country=="Ukraine" & year>1952 & year<1988
replace polity2=-7 if country=="Belarus" & year>1952 & year<1988
replace polity2=-6 if country=="Ukraine" & year==1988
replace polity2=-6 if country=="Belarus" & year==1988
replace polity2=-4 if country=="Ukraine" & year==1989
replace polity2=-4 if country=="Belarus" & year==1989
drop _merge
save estimation_file, replace

drop dem 
gen dem=0 if democracy==0
replace dem=1 if democracy==1
gen dem0=1 if democracy==0
replace dem0=0 if democracy==1
/*label define dem 0 "Dictatorship" 1 "Democracy" */

label values dem dem
label define dem0 0 "Dictatorship" 1 "Democracy" 
label values dem0 dem0
gen vdem=0 if v2x_regime!=.
replace vdem=1 if v2x_regime==2 | v2x_regime==3
label var polity2 "Democracy"
label var vdem "Democracy, VDEM"
replace country="Democratic Republic of Congo" if country=="Democratic Republic of the Congo"
replace country="Democratic Republic of Congo" if country=="Zaire"

save estimation_file, replace



/* add IO membership */
gen cowcode=ccode
do vars/member.do
gen member= ALBA +AfricanUnion+ ACS +Agadir+ AGOA +AndeanAFTA+ AndeanComm +ANZCERTA+ APEC+ ArabLeague +ASEAN +ASEANAFTA ///
+ UIAS +UAS +OAU +AU +AUSNZFTA +BA +BSEC +CACO +CAEU+ CAEC +CACM +CArabEU+ CAFTA   ///
+  CO  +CEAO +CCTS +CEFTA +CEN_SAD +CEMAC +CEPGL +SahelBenin+ CES+ CIS +COE +Colombo+ CONDECA  ///
+ COMESA +COMESAPTA+ EACSO +EAC +EACM +EEU +EAEC +EURASEC+ ECCAS+ RCD +ECO +ECALBANIA+ ECALGERIA+ ECANDORRA ///
+ ECBOSNIAHERZEGOVINA +OTHERECAA+ ECCameroon +ECCARIFORUM +ECChile +ECCotedIvoire+ ECCroatia+ ECEgypt ///
+ ECFYRM +ECIceland+ ECIsrael+ ECJordan+ ECLebanon +ECMexico +ECMontenegro+ ECMorocco+ ECNorway+ ECRomania ///
+ ECSAfrica +ECSwitzLiech +ECSyria +ECTunisia +ECTurkey +EFTA +EFTAAA +EFTACHILE +EFTACROATIA+ EFTAEGYPT  ///
+EFTAFYRM +EFTAISRAEL+ EFTAJORDAN +EFTALEBANON +EFTAMEXICO+ EFTAMOROCCO +EFTASACU +EFTASINGAPORE+ EFTASKOREA ///
+ EFTATUNISIA +EFTATURKEY +ECOWAS +ENP +EU +EEA +GAFTA +GATT+ GCC +GUAM +IGADD+ IGAD +IORA +IOC+ LAFTA ///
+ LAIA +LOME+ Mercosur+ MRU +MSG +NAFTA_CUS+ NAFTA+ NATO +NC +OAS +OEEC +OECD +OECS +OSCE +CSCE +PATCRA ///
+ PTA +SPC +SPF+ PIF+ PICTA+ SAARC +SACU+ SADCC+ SADC +SAFTA+ SCO +SELA +ODECA +SICA +SPARTECA +UDEAC ///
+ UDE +UEAC +UEMOA+ UMA +UN +WTO +UNASUR+ YAOUNDE+ UAM +UAMCE+ OCAM +UDAO +UDEAO +COMECON +CMEA +ACTO ///
+ ACT +ACC+ ArcticCouncil +Benelux +LiptakoGourma +OIC +METO +BIMSTEC +CENTO +SEATO +Maphilindo +ASA  ///
+WEU +WNC +CEDC +NORDEFCO+ Visegrad +Salzburg +WarsawPact

drop cowcode ALBA AfricanUnion ACS Agadir AGOA AndeanAFTA AndeanComm ANZCERTA APEC ArabLeague ASEAN ASEANAFTA UIAS UAS OAU AU AUSNZFTA BA BSEC CACO CAEU CAEC CACM CArabEU CAFTA CARIFTA CARICOM AACC CC CO CBSS CEAO CCTS CEFTA CEN_SAD CEMAC CEPGL SahelBenin CES CIS COE Colombo CONDECA COMESA COMESAPTA EACSO EAC EACM EEU EAEC EURASEC ECCAS RCD ECO ECALBANIA ECALGERIA ECANDORRA ECBOSNIAHERZEGOVINA OTHERECAA ECCameroon ECCARIFORUM ECChile ECCotedIvoire ECCroatia ECEgypt ECFYRM ECIceland ECIsrael ECJordan ECLebanon ECMexico ECMontenegro ECMorocco ECNorway ECRomania ECSAfrica ECSwitzLiech ECSyria ECTunisia ECTurkey EFTA EFTAAA EFTACHILE EFTACROATIA EFTAEGYPT EFTAFYRM EFTAISRAEL EFTAJORDAN EFTALEBANON EFTAMEXICO EFTAMOROCCO EFTASACU EFTASINGAPORE EFTASKOREA EFTATUNISIA EFTATURKEY ECOWAS ENP EU EEA GAFTA GATT GCC GUAM IGADD IGAD IORA IOC LAFTA LAIA LOME Mercosur MRU MSG NAFTA_CUS NAFTA NATO NC OAS OEEC OECD OECS OSCE CSCE PATCRA PTA SPC SPF PIF PICTA SAARC SACU SADCC SADC SAFTA SCO SELA ODECA SICA SPARTECA UDEAC UDE UEAC UEMOA UMA UN WTO UNASUR YAOUNDE UAM UAMCE OCAM UDAO UDEAO COMECON CMEA ACTO ACT ACC ArcticCouncil Benelux LiptakoGourma OIC METO BIMSTEC CENTO SEATO Maphilindo ASA WEU WNC CEDC NORDEFCO Visegrad Salzburg WarsawPact

sort iso3n year
drop if iso3n==iso3n[_n-1] & year==year[_n-1]

save estimation_file, replace


/* emergency UN session that year */
gen emergency=0
replace emergency=1 if year ==1956
replace emergency=1 if year ==1958
replace emergency=1 if year ==1960
replace emergency=1 if year ==1967
replace emergency=1 if year ==1980
replace emergency=1 if year ==1981
replace emergency=1 if year ==1982
replace emergency=1 if year ==1997
replace emergency=1 if year ==1998
replace emergency=1 if year ==2000
replace emergency=1 if year ==2001
replace emergency=1 if year ==2002
replace emergency=1 if year ==2003
replace emergency=1 if year ==2004
replace emergency=1 if year ==2007
replace emergency=1 if year ==2009
replace emergency=1 if year ==2017
replace emergency=1 if year ==2018
label var emergency "Emergency session year"

/* major crisis years */ 
 gen koreawar=0
replace koreawar=1 if year==1950 | year==1951 | year==1952 | year==1953
gen vietnamwar=0
replace vietnamwar=1 if year>1963 & year<1974
gen berlin=0
replace berlin=1 if year==1948 | year==1949 | year==1961
gen cuba=0
replace cuba=1 if year==1962
gen afghanwar=0
replace afghanwar=1 if year>1979 & year<1988
gen crisis=0
replace crisis=1 if koreawar==1 | vietnamwar==1 | berlin==1 | cuba==1 | afghanwar==1
 gen hungary=0
replace hungary=1 if year==1956
gen czechoslovakia=0
replace czechoslovakia=1 if year==1968
gen ukraine=0
replace ukraine=1 if year==2014
 replace crisis=1 if hungary==1 | czechoslovakia==1 | ukraine==1

save estimation_file, replace


/* add second/last term dummies */
sort ccode year
merge ccode year using vars/nelda8, nokeep
tab country if _merge==1
/* lastterm is missing for parliamentary, monarchies or unconstrained dictatorships, replace=0 */
replace presidential=presidential[_n-1] if presidential==. & iso3n==iso3n[_n-1] & year>2015
replace lastterm=0 if country=="South Africa"
replace lastterm=1 if country=="South Africa" & year>2003 & year<2008
replace lastterm=1 if country=="South Africa" & year>2013 
replace lastterm=0 if country=="Botswana"
replace lastterm=1 if country=="Botswana" & year>1994 & year<1999
replace lastterm=1 if country=="Botswana" & year>2003 & year<2008
replace lastterm=1 if country=="Botswana" & year>2013 & year<2018
replace presidential=1 if country=="South Africa" & year>1993
replace presidential=1 if country=="Botswana" 
replace lastterm=0 if country=="Argentina" & year>1999
replace lastterm=1 if country=="Argentina" & year>2011 & year<2016  
replace lastterm=1 if country=="Armenia" & year==1997 
replace lastterm=0 if country=="Armenia" & year>1997
replace lastterm=1 if country=="Armenia" & year>2002 & year<2008
replace lastterm=1 if country=="Armenia" & year>2012 & year<2018
replace lastterm=0 if country=="Austria"
replace presidential=0 if country=="Austria"
replace lastterm=0 if country=="Bangladesh"
replace presidential=0 if country=="Bangladesh"
replace lastterm=0 if country=="Benin"
replace lastterm=1 if country=="Benin" & year>2000 & year<2006
replace lastterm=1 if country=="Benin" & year>2010 & year<2016
replace presidential=1 if country=="Bolivia"
replace lastterm=1 if country=="Bolivia"
replace lastterm=0 if country=="Bolivia" & year>2009
replace lastterm=1 if country=="Bolivia" & year>2014 
replace presidential=1 if country=="Brazil"
replace lastterm=0 if country=="Brazil" & year>2011
replace lastterm=1 if country=="Brazil" & year>2013 & year<2017
replace presidential=0 if country=="Bulgaria"
replace lastterm=0 if country=="Bulgaria"
replace lastterm=0 if country=="Burundi"
replace presidential=1 if country=="Burundi"
replace lastterm=1 if country=="Burundi" & year>2009 & year<2015
replace lastterm=1 if country=="Burundi" & year>1997 & year<2003
replace lastterm=0 if country=="Belarus"
replace lastterm=1 if country=="Belarus" & year>2000 & year<2005
replace presidential=1 if country=="Belarus" & year>1993
replace presidential=1 if country=="Cape Verde" 
replace lastterm=0 if country=="Cape Verde"
replace lastterm=1 if country=="Cape Verde" & year>1995 & year<2001
replace lastterm=1 if country=="Cape Verde" & year>2005 & year<2011
replace lastterm=1 if country=="Cape Verde" & year>2015 
replace presidential=1  if country=="Central African Republic"
replace lastterm=0 if country=="Central African Republic"
replace lastterm=1 if country=="Central African Republic" & year>1998 & year<2003
replace lastterm=1 if country=="Central African Republic" & year>2010 & year<2013
replace lastterm=1 if country=="Chile" & year<1973
replace presidential=1 if country=="Chile"
replace lastterm=1 if country=="Chile" & year>1990
replace lastterm=1 if country=="Colombia" 
replace presidential=1  if country=="Colombia"
replace lastterm=0 if country=="Colombia" & year>2001 & year<2006
replace lastterm=0 if country=="Colombia" & year>2009
replace lastterm=1 if country=="Colombia" & year>2013 & year<2018
replace lastterm=1 if country=="Comoros" & year>2010
replace presidential=1  if country=="Comoros"
replace presidential=1  if country=="Congo"
replace presidential=1  if country=="Cuba" & year<1959
replace lastterm=1 if country=="Switzerland"
replace lastterm=0 if country=="Congo"
replace lastterm=1 if country=="Congo" & year>2008 & year<2016
replace lastterm=1 if country=="Costa Rica"
replace lastterm=0 if country=="Croatia"
replace lastterm=1 if country=="Croatia" & year>1996 & year<2000
replace lastterm=1 if country=="Croatia" & year>2004 & year<2010
replace lastterm=0 if country=="Cyprus" 
replace lastterm=1 if country=="Cyprus" & year>2017
replace lastterm=1 if country=="Cyprus" & year>1997 & year<2003
replace lastterm=1 if country=="Cyprus" & year>1982 & year<1987
replace lastterm=0 if country=="Dominican Republic" & year<1992
replace lastterm=1 if country=="Dominican Republic" & year>1995 & year<2005
replace lastterm=0 if country=="Dominican Republic" & year>2004
replace lastterm=1 if country=="Dominican Republic" & year>2007 & year<2012
replace lastterm=1 if country=="Dominican Republic" & year>2015
replace presidential=1  if country=="Ecuador"
replace lastterm=1 if country=="Ecuador"
replace lastterm=0 if country=="Ecuador" & year>2006 & year<2012
replace presidential=1 if country=="El Salvador" & year>1983
replace lastterm=1 if country=="El Salvador" & year>1983
replace presidential=1 if country=="El Salvador"
replace presidential=1 if country=="Finland"
replace lastterm=0 if country=="Finland"
replace lastterm=1 if country=="Finland" & year>2005 & year<2012
replace lastterm=1 if country=="Finland" & year>2017
replace presidential=1 if country=="France" & year>1957
replace lastterm=0 if country=="France" & year>=2012
replace presidential=1 if country=="Georgia" & year<2014
replace presidential=0 if country=="Georgia" & year>2013
replace lastterm=1 if country=="Georgia" & year>2007 & year<2014
replace lastterm=1 if country=="Georgia" & year>1999 & year<2004
replace lastterm=0 if country=="Georgia" & year>2013
replace presidential=1 if country=="Ghana" 
replace lastterm=0 if country=="Ghana" & year>2012
replace presidential=1 if country=="Guatemala" 
replace lastterm=1 if country=="Guatemala" 
replace presidential=1 if country=="Guinea-Bissau"
replace lastterm=0 if country=="Guinea-Bissau"
replace presidential=1 if country=="Guyana" & year>1979
replace lastterm=0 if country=="Guyana"
replace lastterm=1 if country=="Guyana" & year>2005 & year<2012
replace presidential=1 if country=="Haiti"
replace lastterm=1 if country=="Haiti" & year>1986
replace presidential=1 if country=="Honduras"
replace lastterm=1 if country=="Honduras"
replace lastterm=0 if country=="Honduras" & year>2014 & year<2018
replace presidential=0 if country=="Iceland"
replace lastterm=0 if country=="Iceland"
replace presidential=0 if country=="Ireland"
replace lastterm=0 if country=="Ireland"
replace presidential=1 if country=="Indonesia"
replace lastterm=0 if country=="Indonesia"
replace lastterm=1 if country=="Indonesia" & year>2008 & year<2015
replace lastterm=1 if country=="Indonesia" & year==2019
replace presidential=1 if country=="Kenya"
replace lastterm=0 if country=="Kenya" & year<1997
replace lastterm=1 if country=="Kenya" & year>2008 & year<2012
replace lastterm=0 if country=="Kenya" & year>2012
replace lastterm=1 if country=="Kenya" & year>2016
replace presidential=1 if country=="Kyrgyz Republic"
replace lastterm=0 if country=="Kyrgyz Republic" & lastterm==.
replace lastterm=1 if country=="Kyrgyz Republic" & year>2000 & year<2005
replace lastterm=1 if country=="Kyrgyz Republic" & year==2009
replace lastterm=1 if country=="Kyrgyz Republic" & year>2009
replace presidential=1 if country=="Liberia"
replace lastterm=0 if country=="Liberia"
replace lastterm=1 if country=="Liberia" & year>2011 & year<2018
replace presidential=1 if country=="Lithuania"
replace lastterm=0 if country=="Lithuania"
replace lastterm=1 if country=="Lithuania" & year>2013 & year<2019
replace lastterm=1 if country=="Lithuania" & year>2003 & year<2009
replace presidential=0 if country=="Macedonia"
replace lastterm=0 if country=="Macedonia"
replace presidential=1 if country=="Madagascar"
replace lastterm=0 if country=="Madagascar"
replace lastterm=1 if country=="Madagascar" & year>2006 & year<2009
replace lastterm=1 if country=="Madagascar" & year>2008 & year<2014
replace presidential=1 if country=="Malawi"
replace lastterm=0 if country=="Malawi"
replace lastterm=1 if country=="Malawi" & year>1999 & year<2004
replace lastterm=1 if country=="Malawi" & year>2008 & year<2012
replace lastterm=1 if country=="Malawi" & year==2019
replace presidential=1 if country=="Maldives"
replace lastterm=0 if country=="Maldives"
replace presidential=1 if country=="Mali"
replace lastterm=0 if country=="Mali"
replace lastterm=1 if country=="Mali" & year>1996 & year<2002
replace lastterm=1 if country=="Mali" & year>2006 & year<2012
replace lastterm=1 if country=="Mali" & year>2017
replace presidential=1 if country=="Mauritania"
replace lastterm=0 if country=="Mauritania"
replace lastterm=1 if country=="Mauritania" & year>2013
replace presidential=1 if country=="Mexico"
replace lastterm=1 if country=="Mexico"
replace presidential=0 if country=="Moldova" & year>2017
replace lastterm=0 if country=="Moldova"  & year>2012
replace presidential=1 if country=="Mongolia" & year>1990
replace lastterm=0 if country=="Mongolia" & year>1990
replace lastterm=1 if country=="Mongolia"  & year>2000 & year<2005
replace lastterm=1 if country=="Mongolia"  & year>2012 & year<2017
replace presidential=1 if country=="Nicaragua" & year<1986 
replace presidential=1 if country=="Nicaragua" &  year>2017
replace lastterm=0 if country=="Nicaragua"  & year>2009
replace presidential=1 if country=="Niger" 
replace lastterm=0 if country=="Niger"
replace lastterm=1 if country=="Niger" & year>2004 & year<2010
replace lastterm=1 if country=="Niger" & year>2015
replace presidential=1 if country=="Nigeria" 
replace lastterm=0 if country=="Nigeria" &  lastterm==.
replace lastterm=1 if country=="Nigeria" & year==1983
replace lastterm=1 if country=="Nigeria" & year>2002 & year<2007
replace lastterm=1 if country=="Nigeria" & year==2019
replace presidential=1 if country=="Panama" 
replace lastterm=1 if country=="Panama" &  year>1989
replace lastterm=0 if country=="Singapore"
replace presidential=1 if country=="Iran"
replace lastterm=1 if country=="Iran" & year>2016
replace lastterm=1 if country=="Iran" & year>2008 & year<2013
replace lastterm=1 if country=="Iran" & year>2000 & year<2005
replace lastterm=1 if country=="Iran" & year>1993 & year<1997
replace lastterm=1 if country=="Iran" & year>1985 & year<1989
replace presidential=1 if country=="Paraguay"
replace lastterm=1 if country=="Paraguay" & year==1953
replace lastterm=1 if country=="Paraguay" & year>1952 & year<1962
replace lastterm=1 if country=="Paraguay" & year>1990
replace presidential=1 if country=="Peru"
replace lastterm=0 if country=="Peru" &  lastterm==.
replace lastterm=1 if country=="Peru" & year>1990 & year<1992
replace lastterm=1 if country=="Peru" & year>1994 & year<2000
replace lastterm=1 if country=="Peru" & year>2000
replace presidential=1 if country=="Philippines"
replace lastterm=0 if country=="Philippines" &  lastterm==.
replace lastterm=1 if country=="Philippines" & year>1969 & year<1973
replace lastterm=1 if country=="Philippines" & year>1992
replace presidential=1 if country=="Poland" & year>1990
replace lastterm=0 if country=="Poland" &  lastterm==.
replace presidential=1 if country=="Portugal" & year>1976
replace lastterm=0 if country=="Portugal" &  lastterm==.
replace lastterm=1 if country=="Portugal" & year>2010 & year<2016
replace presidential=1 if country=="Romania" & year>1990
replace lastterm=0 if country=="Romania" &  lastterm==.
replace lastterm=1 if country=="Romania" & year>2000 & year<2005
replace lastterm=1 if country=="Romania" & year>2009 & year<2015
replace lastterm=0 if country=="Sao Tome and Principe"  &  lastterm==.
replace lastterm=1 if country=="Sao Tome and Principe" & year>1995 & year<2001
replace lastterm=1 if country=="Sao Tome and Principe" & year>2005 & year<2011
replace presidential=1 if country=="Senegal" 
replace lastterm=0 if country=="Senegal" &  lastterm==.
replace lastterm=1 if country=="Senegal" & year>2006 & year<2012
replace lastterm=1 if country=="Senegal" & year>2019
replace lastterm=0 if country=="Sierra Leone" &  lastterm==.
replace lastterm=1 if country=="Sierra Leone" & year>2001 & year<2007
replace lastterm=1 if country=="Sierra Leone" & year>2012 & year<2018
replace presidential=0 if country=="Slovenia"
replace lastterm=1 if country=="South Korea"
replace presidential=1 if country=="Sri Lanka" & year>1982
replace lastterm=1 if country=="Sri Lanka" & year>1999 & year<2006
replace lastterm=1 if country=="Sri Lanka" & year>2009 & year<2015
replace lastterm=0 if country=="Sri Lanka" & lastterm==.
replace lastterm=0 if country=="Timor" & lastterm==.
replace lastterm=0 if country=="Timor"
replace presidential=1 if country=="Tunisia"
replace lastterm=0 if country=="Tunisia"
replace presidential=1 if country=="Uganda"
replace lastterm=0 if country=="Uganda"
replace lastterm=0 if country=="Ukraine" & lastterm==.
replace lastterm=1 if country=="United States" & year>1972 & year<1975
replace lastterm=0 if country=="United States" &  year>2017
replace lastterm=1 if country=="United States" & year>2012 & year<2017
replace presidential=1 if country=="Uruaguay"
replace lastterm=1 if country=="Uruguay" & year>1985
replace presidential=1 if country=="Venezuela"
replace lastterm=0 if country=="Venezuela" & lastterm==.
replace lastterm=1 if country=="Venezuela" & year>2005 & year<2010
replace presidential=1 if country=="Yugoslavia" & year>1989
replace lastterm=0 if country=="Yugoslavia" & lastterm==.
replace lastterm=1 if country=="Yugoslavia" &  year>2007 & year<2012
replace presidential=1 if country=="Turkey" & year>2016
replace lastterm=0 if country=="Turkey" & year>2016
replace presidential=1 if country=="Zimbabwe" & year>1986
replace lastterm=0 if country=="Zimbabwe" & year>1986
replace presidential=1 if country=="Zambia" 
replace lastterm=0 if country=="Zambia" & lastterm==.
replace lastterm=1 if country=="Zambia" & year>2015
replace lastterm=0 if country=="Yemen"
replace presidential=1 if country=="Yemen"
replace presidential=1 if country=="Uzbekistan"
replace lastterm=0 if country=="Uzbekistan"
replace presidential=1 if country=="Turkmenistan"
replace lastterm=0 if country=="Turkmenistan"
replace presidential=1 if country=="Tajikistan"
replace lastterm=0 if country=="Tajikistan"
replace lastterm=1 if country=="Tajikistan" & year>1999 & year<2004
replace presidential=1 if country=="Kazakhstan"
replace lastterm=0 if country=="Kazakhstan"
replace presidential=1 if country=="Azerbaijan"
replace lastterm=1 if country=="Azerbaijan" & year>1998 & year<2004
replace lastterm=1 if country=="Azerbaijan" & year>2007 & year<2011
replace presidential=1 if country=="Russia" & year>1991
replace lastterm=0 if country=="Russia"
replace lastterm=1 if country=="Russia" & year>1995 & year<2000
replace lastterm=1 if country=="Russia" & year>2018 
replace presidential=1 if country=="Togo"
replace presidential=1 if country=="Tanzania"
replace lastterm=1 if country=="Tanzania" & year>1990 & year<1996
replace lastterm=1 if country=="Tanzania" & year>2000 & year<2006
replace lastterm=1 if country=="Tanzania" & year>2010 & year<2016
replace presidential=1 if country=="Syria"
replace presidential=1 if country=="Sudan"
replace presidential=1 if country=="South Yemen"
replace presidential=1 if country=="Somalia" & year<1991
replace presidential=1 if country=="Rwanda"
replace lastterm=1 if country=="Rwanda" & year>2010 & year<2016
replace presidential=1 if country=="Pakistan" & year>2000 & year<2008
replace lastterm=1 if country=="Pakistan" & year>2001 & year<2008
replace presidential=1 if country=="Namibia"
replace lastterm=1 if country=="Namibia" & year>1997 & year<2005
replace lastterm=1 if country=="Namibia" & year>2009 & year<2015
replace presidential=1 if country=="Mozambique"
replace lastterm=1 if country=="Mozambique" & year>1999 & year<2005
replace lastterm=1 if country=="Mozambique" & year>2009 & year<2015
replace presidential=1 if country=="Montenegro"
replace lastterm=1 if country=="Montenegro" & year>2012 & year<2018
replace presidential=1 if country=="Mali"
replace lastterm=1 if country=="Mali" & year>2006 & year<2012
replace lastterm=1 if country=="Mali" & year>2017
replace presidential=1 if country=="Iraq" & year> 1958 & year<2003
replace presidential=1 if country=="Afghanistan" & year> 2003
replace lastterm=1 if country=="Afghanistan" & year>2008 & year<2015
replace presidential=1 if country=="Gambia"
replace presidential=1 if country=="Gabon"
replace lastterm=0 if country=="Gabon"
replace presidential=1 if country=="Djibouti"
replace lastterm=0 if country=="Djibouti"
replace presidential=1 if country=="Egypt" & year>1953
replace presidential=1 if country=="Algeria"
replace presidential=1 if country=="Angola"
replace lastterm=0 if country=="Algeria"
replace lastterm=1 if country=="Algeria" & year>2002 & year<2010
replace presidential=1 if country=="Lebanon"
replace lastterm=1 if country=="Lebanon"
replace lastterm=0 if country=="Bosnia and Herzegovina"
replace presidential=1 if country=="Democratic Republic of Congo"
replace lastterm=1 if country=="Democratic Republic of Congo" & year>2011 & year<2019
replace presidential=1 if country=="Benin"
replace presidential=1 if country=="Chad"
replace presidential=1 if country=="Burkina Faso"
replace lastterm=1 if country=="Burkina Faso" & year>1997 & year<2006
replace lastterm=0 if lastterm==. 
label var lastterm "Lame duck"
replace presidential=0 if presidential==.
drop _merge
save estimation_file, replace


/* additional variables */
gen loggdpsize=log10(gle_gdp)
label var loggdpsize "GDP, log"
gen leaderage=entryage+count
label var leaderage "Leader's age"
gen intercareer=0 if career!=""
replace intercareer=1 if career=="international development or EU" 
replace intercareer=1 if career2=="international development or EU"
label var intercareer "Prior international career"

sort ccode year
merge ccode year using vars/vdem_divided, nokeep
sort ccode year
drop _merge
gen divided=0 if v2psnatpar_ord!=.
replace divided=1 if v2psnatpar_ord==1
label var divided "Divided government"

/* world and regional leaders indicators */
drop count
gen count=1
by year, sort: egen allspeakers=sum(count)
by year, sort: egen allruler=sum(executive)
replace allruler=allruler-1 if executive==1
gen worldruler=allruler/allspeakers
replace worldruler=worldruler*100
save estimation_file, replace


/* add unsc regions */
clear
use estimation_file
sort ccode year
merge ccode year using vars/UNvote, nokeep
drop _merge
save estimation_file, replace


/* missing values of unsc region */
sort iso3n year
replace unsc_region= unsc_region[_n-1] if unsc_region==. & iso3n==iso3n[_n-1] & year>2015
replace unsc_region=2 if country=="Yemen"
replace unsc_region=1 if country=="Gambia"
replace unsc_region=5 if country=="Haiti"
gen un_reg= unsc_region
replace un_reg=3 if country=="Armenia" | country=="Azerbaijan" | country=="Georgia"
replace un_reg=2 if country=="Cyprus"
replace un_reg=4 if country=="Turkey"
replace un_reg=4 if country=="Israel" & year>1999

/* regional rulers */
by un_reg year, sort: egen regspeaker=sum(count)
by un_reg year, sort: egen regruler2=sum(executive)
replace regruler2=regruler2-1 if executive==1 & regruler2>1
gen regionruler=regruler2/regspeaker
replace regionruler=regionruler*100
label var worldruler "World leaders"
label var regionruler "Regional leaders"


/* other leaders attendance lagged by one year */
sort ccode year	
gen worldrulerl1=worldruler[_n-1] if ccode==ccode[_n-1]
label var worldrulerl1 "World leaders, t-1"

save estimation_file, replace

/* anniversary years */
gen anniv=0
replace anniv=1 if year==1995 | year==2000  | year==2010 | year==1990  | year==2005 | year==2015
label var anniv "Anniversary year"
label var v2x_regime "Democracy, V-Dem"
label var presidential "Presidentialial"
label var latent_personalism "Personalism"
label var g77 "G77"
label var member "IO membership"
label var v2xnp_pres "Power concentration"
label var v2x_polyarchy "Democracy index"


/* leaders attend as share of world economy */
gen econleader=loggdpsize if executive==1
by year, sort: egen worldecon=sum(loggdpsize)
by  year, sort: egen worldeconlead=sum(econleader) 
replace worldeconlead=worldeconlead-loggdpsize if executive==1
gen leadworldeconshare= worldeconlead/worldecon
label var leadworldeconshare "Leaders, economic share"

/* share of leaders from top10 economies */
gsort year -loggdpsize
by year : gen top10econ = 1 if _n<=10
gen top10econlead=1 if top10econ == 1 & executive==1
replace top10econlead=0 if top10econlead==. & loggdpsize!=.
by year, sort: egen top10leadnum=sum(top10econlead)
replace top10leadnum=top10leadnum-1 if executive==1 & top10econ==1
gen top10leadnum2=top10leadnum/10
label var top10leadnum2 "Leaders, Top10 Economies"

/* share of leaders from top20 economies */
gsort year -loggdpsize
by year : gen top20econ = 1 if _n<=20
gen top20econlead=1 if top20econ == 1 & executive==1
replace top20econlead=0 if top20econlead==. & loggdpsize!=.
by year, sort: egen top20leadnum=sum(top20econlead)
replace top20leadnum=top20leadnum-1 if executive==1 & top20econ==1
gen top20leadnum2=top20leadnum/20
label var top20leadnum2 "Leaders, Top20 Economies"

/* lagged leaders */
sort ccode year	
gen top20leadnum2l1=top20leadnum2[_n-1] if ccode==ccode[_n-1]
gen top10leadnum2l1=top10leadnum2[_n-1] if ccode==ccode[_n-1]
gen leadworldeconsharel1=leadworldeconshare[_n-1] if ccode==ccode[_n-1]
label var top20leadnum2l1 "Leaders, Top20 Economies"
label var top10leadnum2l1 "Leaders, Top10 Economies"
label var leadworldeconsharel1 "Leaders, economic share"

/* DATA/COW/UCDP/PRIO Armed Conflict Dataset  */			 
gen war=0 if anywar!=.
replace war=1 if intensity_level==2
label var war "Military conflict"

/* labels for leader background */
label var intercareer "Prior international career"
label var minforeign "ex MFA"
label var foreignedu "Education abroad"
label var leaderage "Leader’s age"
label var anywar "Military conflict"

/* unsc seat */
sort ccode year
merge ccode year using vars/unsc, nokeep
drop _merge
drop count


/* unga resolutions sponsors */

sort iso3n year
merge iso3n year using vars/primary_sponsors, nokeep 
drop _merge

/* important global events */
gen globalevent=0
replace globalevent=1 if year==1956 | year==1973 | year==1974 | year==1985 | year==1986 | year==2001 | year==2008 | year==2015
label var globalevent "Global event"

/* post2000 dummy */
gen post2000=0
replace post2000=1 if year>1999
label var post2000 "Post Millenium Summit"
save estimation_file, replace

///***********************************************///
/* create annual-level file for Table 3 analyses */
///***********************************************///
gen regionruler1=regionruler if un_reg==1
gen regionruler2=regionruler if un_reg==2
gen regionruler3=regionruler if un_reg==3
gen regionruler4=regionruler if un_reg==4
gen regionruler5=regionruler if un_reg==5
collapse (mean) worldruler regionruler1 regionruler2 regionruler3 regionruler4 regionruler5, by(year)
save output/splines, replace

///***********************************************///
/*           create the main estimation file */
///***********************************************///

clear
use estimation_file

keep year ruler speaker executive MFA country iso3n ccode executive   worldrulerl1 leadworldeconsharel1    top20leadnum2l1  ///
  vdem globalevent v2xnp_pres west region africa asia cee latam mena postsoviet democracy ///
loggdpsize logdistcap emergency  member  anniv    lastterm  ///
divided   communist unsc2  latent_personalism un_reg worldruler ///
logpop log10income anywar  e_migdpgro intercareer minforeign foreignedu leaderage ///
  gwf_personal gwf_monarchy gwf_military gwf_party  numresols1 numresols1_2 post2000
  
save estimation_file, replace
  
/*** generate time splines, locate and have btscs.ado installed ***/
/* https://www-personal.umich.edu/~wmacmill/stata/btscs/btscs-a-binary-time.pdf */
gen count=year-1945
btscs executive year ccode, g(spline) nspline(3)



save estimation_file, replace

/* end */

